package ipad.asia.controller;

import ipad.asia.dao.ProductDao;
import ipad.asia.entity.Product;
import ipad.asia.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductDao productDao;

    @RequestMapping("/product/save")
    public void add(@RequestParam(value = "name",required = false) String name,
                            @RequestParam(value = "price",required = false) String price,
                            @RequestParam(value = "describe",required = false) String describe,
                            @RequestParam(value = "photo",required = false) String photo,
                            @RequestParam(value = "shopId",required = false) String shopId,
                            @RequestParam(value = "kind",required = false) String kind,
                    @RequestParam(value = "color",required = false) String color,
                    @RequestParam(value = "photo1",required = false) String photo1,
                    @RequestParam(value = "photo2",required = false) String photo2,
                    @RequestParam(value = "photo3",required = false) String photo3,
                    @RequestParam(value = "photo4",required = false) String photo4,
                    @RequestParam(value = "desc_photo1",required = false) String desc_photo1,
                    @RequestParam(value = "desc_photo2",required = false) String desc_photo2,
                    @RequestParam(value = "desc_photo3",required = false) String desc_photo3,
                    @RequestParam(value = "desc_photo4",required = false) String desc_photo4,
                    @RequestParam(value = "desc_photo4",required = false) String desc_photo5,
//                            @RequestParam(value = "discount",required = false) String discount,
                            @RequestParam(value = "under",required = false) String under,
                            @RequestParam(value = "number",required = false) String number,
                            @RequestParam(value = "haveDiscount",required = false) String haveDiscount,
                            @RequestParam(value = "haveSell",required = false) String haveSell)  {
            Product product=new Product();
            System.out.println("shopId"+shopId+"---------"+"price"+price);
        product.setName(name).setPhoto(photo).setDescribes(describe).setPhoto(kind).setColor(color)
                .setPhoto1(photo1).setPhoto2(photo2).setPhoto3(photo3).setPhoto4(photo4)
                .setDescPhoto1(desc_photo1).setDescPhoto2(desc_photo2).setDescPhoto3(desc_photo3)
                .setPhoto4(desc_photo4).setDescPhoto5(desc_photo5);
        if(price==null){product.setPrice(9999999F);}else {product.setPrice(Float.parseFloat(price));}
        if(shopId==null){product.setShopId(0);}else {product.setShopId(Integer.parseInt(shopId));}
//        if(discount==null){product.setDiscount(1.0F);}else {product.setDiscount(Float.parseFloat(discount));}
        if(under==null){product.setUndercarriage(0);}else {product.setUndercarriage(Integer.parseInt(under));}
        if(number==null){product.setNumber(1);}else {product.setNumber(Integer.parseInt(number));}
        if(haveDiscount==null){product.setHaveDiscount(0);}else {product.setHaveDiscount(Integer.parseInt(haveDiscount));}
        if(haveSell==null){product.setHaveSell(0);}else {product.setHaveSell(Integer.parseInt(haveSell));}
        productService.save(product);}

    @PostMapping("/product/saveObj")
    public void addObj(@RequestBody Product product){
        productDao.save(product);
    }

    @RequestMapping("/product/get/id")
    @Transactional
    public Product getById(@RequestParam("id") int id) throws Exception {
        return productDao.findById(id);
    }

    @RequestMapping("/product/get/ShopId")
    public List<Product> getShopId(@RequestParam(value = "ShopId",required = false)int shopId) throws Exception {
            return productDao.findByShopId(shopId);
        }

    @RequestMapping("/product/get/name")
    public List<Product> findByName(@RequestParam(value = "name",required = false)String name){
       return productDao.findByNameLike("%"+name+"%");
    }

    /**
     * 测试json转换
      * @return
     */
//    @RequestMapping("/test1")
//    public String test1(){
//        String vivo = findByName("vivo");
//        List<Product> product=JSON.parseArray(vivo,Product.class);
//        for(int i=0;i<product.size();i++){
//            System.out.println(product.get(i).getId()+product.get(i).getName());
//        }
//        return vivo;
//    }
    @RequestMapping("/product/get/all")
    public List<Product> all(){
        return productService.findAll();
    }
    @RequestMapping("/product/get")
    public List<Product> findByPrice(@RequestParam(value = "down",required = false)int down,
                                     @RequestParam(value = "up",required = false)int up){
        return productService.findByPriceBetween(down,up);
    }

    @RequestMapping("/product/get/kind")
    public List<Product> findByKind(@RequestParam(value = "kind",required = false)String kind){
        return productService.findByKind(kind);
    }

    @RequestMapping("/product/get/haveDiscount")
    public List<Product> findByIsDiscount(@RequestParam(value = "flag",required = false)int flag){
        return productService.findByHaveDiscount(flag);
    }
    //是否下架
//    @RequestMapping("/product/get/under/{flag}")
//    public List<Product> findByUndercarriage(@PathVariable(value = "flag",required = false)int flag){
//        return productService.findByUndercarriage(flag);
//    }
//    //是否卖出
//    @RequestMapping("/product/get/haveSell/{flag}")
//    public List<Product> findByHaveSell(@PathVariable(value = "flag",required = false)int flag){
//        return productService.findByHaveSell(flag);
//    }
    //删除
    @Transactional
    @RequestMapping("/product/delete")
    public void DeleteById(@RequestParam(value = "id",required = false)int id){
         productService.deleteById(id);
        System.out.println("ok");
    }

    @Transactional
    @RequestMapping("/product/updateNumber")
    public void updateNumber(@RequestParam(value = "number",required = false) int number,
                             @RequestParam(value = "id",required = false) int id){
         productDao.updateNumber(number, id);
   }

    @Transactional
    @RequestMapping("/product/updateDiscount")
    public void updateDiscount(@RequestParam(value = "haveDiscount",required = false) int haveDiscount,
                               @RequestParam(value = "id",required = false) int id){
        productDao.updateDiscount(haveDiscount, id);
    }

    @Transactional
    @RequestMapping("/product/update")
    public void update(@RequestParam(value = "name",required = false) String name,
                       @RequestParam(value = "price",required = false) Float price,
                       @RequestParam(value = "number",required = false) int number,
                       @RequestParam(value = "color",required = false) String color,
                       @RequestParam(value = "kind",required = false) String kind,
                       @RequestParam(value = "haveDiscount",required = false) int haveDiscount,
                       @RequestParam(value = "photo",required = false) String photo,
                       @RequestParam(value = "id",required = false) int id){
        productDao.update(name, price, number, color, kind, haveDiscount, photo,id);
    }
}
